package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.PrivateMessage;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.AttentionQueryObject;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public void saveOrUpdate(User user) {
        if (user.getId() != null) {
            userMapper.updateByPrimaryKey(user);

        }else{
            if (userMapper.CheckEmailAndPassword(user.getEmail(),null) == null) {
                user.setHeadImgUrl("//upload/fd72cbec-2d41-4462-9287-fec93f12cc24.jpg");
                user.setGender(User.GENDER_SECRET);
                user.setCoverImgUrl("/upload/f7b178c5-dc83-4743-b120-90bfb24bbb9a.jpeg");
                userMapper.insert(user);
            }else{
                throw new RuntimeException("账号已存在");
            }
        }


    }

    @Override
    public Object login(String email, String password) {
        if (password!=null&&email!=null) {
            User user = userMapper.CheckEmailAndPassword(email, password);

            if (user != null) {
                UserContext.setCurrentUser(user);
                return user;
            }else{
                throw new RuntimeException("账号或密码错误");
            }

        }else {
            throw  new RuntimeException("请输入账号或密码");
        }

    }

    @Override
    public PageInfo<User> query(QueryObject qo) {

        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<User> list =userMapper.selectForList(qo);

        return new PageInfo(list);
    }

    @Override
    public User getUserByParamKey(Long id) {

        User user = userMapper.selectByPrimaryKey(id);
        /**
         * 关注数和粉丝数
         */
        Integer fans = userMapper.selectcountFans(id);//粉丝
        Integer attentions = userMapper.selectcounttattentions(id);
        user.setFans(fans);
        user.setAttention(attentions);
        return user;

        //return userMapper.selectByPrimaryKey(id);
    }

    /**
     * wq
     * @param companionId
     * @return
     */
    @Override
    public Boolean selectByCompanionId(Long companionId) {
        Long uId = UserContext.getCurrentUser().getId();
        return userMapper.selectByCompanionId(companionId,uId);
    }

    @Override
    public List<User> selectAll() {
        return userMapper.selectAll(UserContext.getCurrentUser().getId());
    }


    @Override
    public List<User> selectByEmailAndNickName(String str) {
        String keyword = str.trim();
       List<User> list =  userMapper.selectByEmailAndNickName(keyword);
        return list;
    }

    /**qyq
     * 是否关注了
     * @param qo
     * @return
     */
    public Integer selectAttention(AttentionQueryObject qo) {
        return userMapper.selectAttention(qo.getAttentionuserId(),qo.getBeattentionuserId());
    }

    /**
     * 修改关注状态
     * @param attentionuserId
     * @param beattentionuserId
     * @return
     */
    public int changeAttention(Long attentionuserId, Long beattentionuserId) {
        Integer i = userMapper.selectAttention(attentionuserId,beattentionuserId);
        if (i > 0) {
            userMapper.deleteAttention(attentionuserId,beattentionuserId);
            return 1;
        }else{
            userMapper.insertAttention(attentionuserId,beattentionuserId);
            return 0;
        }
    }

    @Override
    public int getFansNum(Long userId) {
        return userMapper.selectcounttattentions(userId);
    }
}
